package com.gnetek.netty.filter;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import javafx.scene.control.TextFormatter;

/**
 * @Description 数字字符过滤器
 * @Author Darren Huang
 * @Date 2022-11-22 14:52
 */
public class HexTextFilter extends LengthTextFilter {

    public HexTextFilter(){};
    public HexTextFilter(int maxLength){
        this.maxLength = maxLength;
    }

    @Override
    public TextFormatter.Change apply(TextFormatter.Change change) {
        change = super.apply(change);
        if(change!=null){
            String newText = change.getControlNewText();
            if(StrUtil.isBlank(newText) || HexUtil.isHexNumber("0x"+newText)){
                return change;
            }
        }
        return null;
    }
}

